registerdshields = {}

local tick = 0
function shieldtick()
	local Time = CurTime()
	if (Time > tick) then
		for number,core in pairs(servertable["ship"]["shipcores"] ) do 
			if core:IsValid() then
				if not core.dmg then core.dmg = 0 end
				if not core.lastdamage then core.lastdamage = CurTime() end
				if (core.lastdamage < CurTime()) then
					local entid = core:EntIndex( )
					local ownerid = servertable["entitys"][entid]["owner"]
					local ply = player.GetByUniqueID(ownerid)
					if ply then
						if ply.infight then
							ply.infight = false
							AcualizeMusic(ply)
						end
					end
				end
				if (core.dmg > 0) then
					util.ScreenShake(core:GetPos(), 100, 5, 1, 300 )
					local difference = core.shield - core.dmg
					core.shield = difference
					local constraints = constraint.GetAllConstrainedEntities(core) 
					local ply = player.GetByUniqueID(servertable["entitys"][core:EntIndex() ]["owner"])
					if ply then
						if not ply.infight then 
							ply.infight = true
							AcualizeMusic(ply)
						end
					end
					core.lastdamage = CurTime() + 60
					if (core.shield < 0) then
						core.health = core.health + core.shield
						core.shield = 0
						if constraints then
							for _,ent3 in pairs(constraints) do 
								if ent3:IsValid() then
									local effectdata = EffectData()
									effectdata:SetStart( ent3:GetPos() )
									effectdata:SetOrigin( ent3:GetPos() )
									effectdata:SetScale( 30 )
									effectdata:SetMagnitude( 40 )
									effectdata:SetEntity(ent3)
									util.Effect( "hit", effectdata )
								end
							end
						end
					else
						if constraints then
							for _,ent3 in pairs(constraints) do 
								if ent3:IsValid() then
									local effectdata = EffectData()
									effectdata:SetStart( ent3:GetPos() )
									effectdata:SetOrigin( ent3:GetPos() )
									effectdata:SetScale( 30 )
									effectdata:SetMagnitude( 40 )
									effectdata:SetEntity(ent3)
									util.Effect( "shield_hit", effectdata )
								end
							end
						end
					end
					if (core.health < 1) then
						if constraints then
							for _,ent3 in pairs(constraints) do 
								if ent3:IsValid() then
									local effectdata = EffectData()
									effectdata:SetStart( ent3:GetPos() )
									effectdata:SetOrigin( ent3:GetPos() )
									effectdata:SetScale( 30 )
									effectdata:SetMagnitude( 40 )
									effectdata:SetEntity(ent3)
									util.Effect( "Explosion", effectdata )
									ent3:Remove()
								end
							end
						end
						local effectdata = EffectData()
						effectdata:SetStart( core:GetPos() )
						effectdata:SetOrigin( core:GetPos() )
						effectdata:SetScale( 5 )
						effectdata:SetMagnitude( 10 )
						effectdata:SetEntity(core)
						util.Effect( "core_explode", effectdata )
					end
					core.dmg = 0
				end
			else
				servertable["ship"]["shipcores"][number] = nil
			end
		end
		tick = Time + 1	
	end
end
hook.Add("Tick", "shieldtick", shieldtick)  

function Redirectdamage(clr,unneeded ,unneeded2 ,unneeded3,info)
	Shieldhit(info,clr)
end
hook.Add("EntityTakeDamage", "Redirectdamage", Redirectdamage)  

function Shieldhit(info,clr)
	local clrpos = clr:GetPos()
	if not clr:IsPlayer() then 	
		local damagedone = false
		if clr:IsValid() then
			local clrentindex = clr:EntIndex( )
			if servertable["entitys"][clrentindex] then 
				if servertable["entitys"][clrentindex]["owner"] then
					for number,core in pairs(servertable["ship"]["shipcores"]) do 
						if core:IsValid() then
							if (servertable["entitys"][core:EntIndex()]["owner"] == servertable["entitys"][clr:EntIndex( )]["owner"]) then
								if not core.dmg then core.dmg = 0 end
								core.dmg = core.dmg + info:GetDamage()
								damagedone = true
								break
							end
						else
							servertable["ship"]["shipcores"][number] = nil
						end
					end
				end
			end
		end
	end
end

